package com.int_tech.station.service;

import com.int_tech.station.dao.StationDao;
import com.int_tech.station.entity.Station;
import com.int_tech.user.dao.UserDao;
import com.int_tech.utils.CarEnum;
import com.int_tech.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by Administrator on 2017/9/22.
 */
@Service
public class StationService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private StationDao stationDao;

    //分页显示条数
    @Value("${size}")
    private int size;
    /**
    * TODO 添加场站信息
    * @param station
    * @return Result
    * @author dmy
    */
    public Result saveStation(Station station){
        try {
            //用户id对象不为空且类型为客户
            if (userDao.findOne(station.getClientId()).getType()!=2){
                return Result.build(201,"客户不存在");
            }else {
                station.setCreateTime(new Date());
                station.setUpdateTime(new Date());
                stationDao.save(station);
                return Result.ok();
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    /**
    * TODO 根据场站id逻辑删除
    * @param id
    * @return Result
    * @author dmy
    */
    @Transactional
    public Result deleteStationByid(Integer id){
        try {
            Station station = stationDao.findOne(id);
            station.setIsDel(CarEnum.OFF.getCode());
            stationDao.save(station);
            return Result.ok();
        } catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }
    /**
    * TODO 修改用户信息
    * @param station
    * @return Result
    * @author dmy
    */
    public Result updateStation(Station station){
        try {
            station.setUpdateTime(new Date());
        		stationDao.save(station);
        		return Result.ok();
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    /**
    * TODO 根据用户id查询场站信息
    * @param clientId
    * @return Result(Station)
    * @author dmy
    */
    public Result findStationByid(Integer clientId){
        try {
        	return Result.ok(stationDao.findOne(clientId));
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    /**
    * TODO 查询所有场站信息
    * @return Result(List<Station>)
    * @author dmy
    */
    public  Result findAllStation(){
        try {
        	return Result.ok(stationDao.findStationByIsDel(CarEnum.ON.getCode()));
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    /**
    * TODO 分页查询场站信息
    * @param currentPage 当前页
    * @return Result(List<Station>)
    * @author dmy
    */
    public Result fingByPage(int currentPage){
        try {
            PageRequest pageRequest = new PageRequest(currentPage-1,size);
            return Result.ok(stationDao.findStationByIsDel(pageRequest,CarEnum.ON.getCode()));
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }
    /**
    * TODO 根据客户id查询场站信息
    * @param clientId,isDel
    * @return Result(List<Station>)
    * @author dmy
    */
    public Result findStationByClientId(int clientId){
        try {
           return Result.ok( stationDao.findStationByClientIdAndIsDel(clientId,CarEnum.ON.getCode()));
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    /**
    * TODO 根据场站名查询场站信息
    * @param name
    * @return Result(Station)
    * @author dmy
    */
    public Result findStationByName(String name){
        try {
        		return 	Result.ok(stationDao.findStationByNameAndIsDel(name,CarEnum.ON.getCode()));
        } catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }
    /**
    * TODO 分页
    * @param
    * @return
    * @author dmy
    */
    /**
     * TODO 分页
     * @param pageable
     * @return
     * @author dmy
     */
    public Page<Station> getCarByPageable(Pageable pageable, int clientId){
        Page<Station> carPage = stationDao.findByClientIdAndIsDel(pageable, clientId, CarEnum.ON.getCode());
        return carPage;
    }

    /**
     * TODO 分页
     * @param pageable
     * @return
     * @author dmy
     */
    public Page<Station> getCarByPageable(Pageable pageable){
        Page<Station> carPage = stationDao.findByIsDel(pageable,CarEnum.ON.getCode());
        return carPage;
    }

}
